package com.quanquan.management.service;

import DTO.PageDTO;
import com.quanquan.management.core.service.GenericService;
import com.quanquan.management.dto.SysRoleDTO;
import com.quanquan.management.entity.SysRole;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.repository.SysRoleRepository;
import com.quanquan.management.request.SysRoleRequestBody;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.DateUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class SysRoleService extends GenericService<SysRole, String> {

    @Autowired
    private SystemLogService systemLogService;
    @Autowired
    private SysRoleRepository sysRoleRepository;

    @Autowired
    private SysPermissionRoleService sysPermissionRoleService;

    @PostConstruct
    private void init() {
        setGenericRepository(sysRoleRepository);
    }


    public PageDTO<SysRoleDTO> list(PageRequestBody requestBody) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "查询角色列表";
        systemLogService.sendSystemLog(sysUser, remark);

        Integer page = requestBody.getPage();
        Integer size = requestBody.getSize();

        Pageable pageable = PageRequest.of(page, size);
        SysRole sysRole = new SysRole();

        Example<SysRole> example = Example.of(sysRole);
        Page<SysRole> findAll = sysRoleRepository.findAll(example, pageable);
        List<SysRole> all = findAll.getContent();
        List<SysRoleDTO> allDto = new ArrayList<>();
        for (SysRole sysRole1 : all) {
            SysRoleDTO sysRoleDTO = new SysRoleDTO();
            sysRoleDTO.setId(sysRole1.getId());
            sysRoleDTO.setName(sysRole1.getName());
            String regTime = DateUtils.dateToString(sysRole1.getRegTime(), "yyyy-MM-dd HH:mm:ss");
            sysRoleDTO.setRegTime(regTime);
            String updateTime = DateUtils.dateToString(sysRole1.getUpdateTime(), "yyyy-MM-dd HH:mm:ss");
            sysRoleDTO.setUpdateTime(updateTime);
            allDto.add(sysRoleDTO);
        }
        PageDTO<SysRoleDTO> objectPageDTO = new PageDTO<>();
        objectPageDTO.setList(allDto);
        objectPageDTO.setPage(page);
        objectPageDTO.setPageSize(size);
        objectPageDTO.setTotalPage(findAll.getTotalPages());
        objectPageDTO.setTotal(findAll.getTotalElements());

        return objectPageDTO;
    }

    public boolean addRole(SysRoleRequestBody requestBody) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "添加新角色";
        systemLogService.sendSystemLog(sysUser, remark);

        if (null == requestBody) {
            return false;
        }
        String name = requestBody.getName();
        SysRole sysRole = new SysRole();
        sysRole.setRegTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRole.setName(name);
        sysRoleRepository.saveAndFlush(sysRole);
        //生成角色与权限的关系
        sysPermissionRoleService.addPermissionRole(sysRole.getId(), requestBody.getPermissionIds());
        return true;
    }


    /**
     * 编辑查询角色
     *
     * @param id
     * @return
     */
    public SysRoleDTO findById(String id) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "查询角色详情列表";
        systemLogService.sendSystemLog(sysUser, remark);

        SysRole sysRole = sysRoleRepository.findById(id).get();
        SysRoleDTO sysRoleDTO = new SysRoleDTO();
        sysRoleDTO.setId(sysRole.getId());
        sysRoleDTO.setName(sysRole.getName());
        String regTime = DateUtils.dateToString(sysRole.getRegTime(), "yyyy-MM-dd HH:mm:ss");
        sysRoleDTO.setRegTime(regTime);
        String updateTime = DateUtils.dateToString(sysRole.getUpdateTime(), "yyyy-MM-dd HH:mm:ss");
        sysRoleDTO.setUpdateTime(updateTime);

        List<String> permissionIds = sysPermissionRoleService.queryPermissionIds(id);
        sysRoleDTO.setPermissionIds(permissionIds);
        return sysRoleDTO;
    }


    /**
     * 角色绑定权限
     *
     * @param requestBody
     * @return
     */
    public boolean update(SysRoleRequestBody requestBody) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String remark = "更新角色信息";
        systemLogService.sendSystemLog(sysUser, remark);

        if (null == requestBody) {
            return false;
        }
        //角色id
        String id = requestBody.getId();
        //角色的名称
        String name = requestBody.getName();
        List<String> permissionIds = requestBody.getPermissionIds();

        //生成角色与权限的关系
        sysPermissionRoleService.addPermissionRole(id, permissionIds);

        //根据id
        SysRole sysRole = sysRoleRepository.findById(id).get();
        sysRole.setName(name);
        sysRole.setUpdateTime(new Date());
        sysRoleRepository.save(sysRole);

        return true;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    public boolean delete(String id) {

        sysRoleRepository.removeShip(id);

        return true;
    }
}
